import { defineStore } from "pinia";
import {
  getTopData,
  getLeftTreeData,
  getTheadList,
  getTbodyData,
} from "@/services/indexManagement.service";
export const indexManagement = defineStore("index-management", {
  state: () => {
    return {
      kpiTotal: "-", //指标总数
      releaseTotal: "-",
      treeData: [],
      dateList: [],
      columns: [],
      tableData: [],
      previewForm: {
        kpiCode: "",
        kpiName: "",
        kpiTypeName: "",
        kpiCycle: "",
        releaseTime: "",
        addDate: "",
        className: "",
        technologyDesc: "",
        busiDesc: "",
      },
      selected: [],
      pageNo: 1,
      pageSize: 15,
      total: 0,
      searchName: "",
      kpiCycle: "",
      kpiTypeCodeList: [],
      kpiCycleList: [],
      addDate: undefined,
      backKpiTypeCodeList: [],
      backKpiCycleList: [],
      backAddDate: undefined,
      everyMonth: undefined,
      everyDay: null,
      backEveryMonth: undefined,
      backEveryDay: null,
      slotHeadName: [
        {
          slot: "kpiTypeName",
          title: "指标类型",
          icon: "filter",
        },
        {
          slot: "kpiCycle",
          title: "指标周期",
          icon: "filter",
        },
        {
          slot: "releaseTime",
          title: "发布时间",
          icon: "search",
        },
        {
          slot: "addDate",
          title: "上架时间",
          icon: "search",
        },
      ],
    };
  },
  getters: {
    releaseTime() {
      let time = "";
      if (this.everyMonth && this.everyDay) {
        time = "每月" + this.everyMonth + this.everyDay;
      } else if (!this.everyMonth && this.everyDay) {
        time = "每日" + this.everyDay;
      } else if (this.everyMonth && !this.everyDay) {
        time = "每月" + this.everyMonth;
      } else {
        time = "";
      }
      return time;
    },
    classLv1Code() {
      let v1Code = "";
      if (this.selected.length > 0) {
        if (this.selected[0].includes("-")) {
          v1Code = this.selected[0].split("-")[0];
        } else {
          v1Code = this.selected[0];
        }
      }
      return v1Code;
    },
    classLv2Code() {
      let v2Code = "";
      if (this.selected.length > 0 && this.selected[0].includes("-")) {
        v2Code = this.selected[0].split("-")[1];
      }
      return v2Code;
    },
  },
  actions: {
    disposeTreeData(arr, parentId) {
      let data = [];
      arr.forEach((item) => {
        let obj = {};
        let total = item.total || 0;
        obj.title = item.title + "（" + total + "）";
        obj.key = parentId ? parentId + "-" + item.classCode : item.classCode;
        if (item.children && item.children.length > 0) {
          obj.children = this.disposeTreeData(item.children, item.classCode);
        }
        data.push(obj);
      });
      return data;
    },
    async getTopDataApi() {
      const res = await getTopData();
      if (res && res.code === 200) {
        this.kpiTotal = res?.result?.kpiTotal || "-";
        this.releaseTotal = res?.result?.releaseTotal || "-";
      } else {
        this.kpiTotal = "-";
        this.releaseTotal = "-";
      }
    },
    async getLeftTreeDataApi() {
      const res = await getLeftTreeData();
      if (res && res.code === 200) {
        this.treeData = this.disposeTreeData(
          res?.result?.kpiClassifyList || []
        );
        this.dateList =
          Array.isArray(res?.result?.kpiCycleList) &&
          res?.result?.kpiCycleList.map((item) => {
            let total = item.total || 0;
            return {
              title: item.title + "（" + total + "）",
              key: item.kpiCycle,
            };
          });
      } else {
        this.treeData = [];
        this.dateList = [];
      }
    },
    disposeHeadData(arr) {
      let colums = arr.map((item, index) => {
        let obj = {
          key: item.field,
          dataIndex: item.field,
          align: "left",
          ellipsis: true,
        };
        if (index === 0) {
          obj.fixed = "left";
          obj.align = "left";
          obj.width = 150;
          obj.title = item.fieldName;
        } else if (item.field === "kpiTypeName") {
          obj.slots = { title: "kpiTypeName" };
          obj.width = 150;
        } else if (item.field === "kpiCycle") {
          obj.slots = { title: "kpiCycle" };
          obj.width = 150;
          obj.scopedSlots = { customRender: "dateType" };
        } else if (item.field === "releaseTime") {
          obj.slots = { title: "releaseTime" };
          obj.width = 150;
        } else if (item.field === "addDate") {
          obj.slots = { title: "addDate" };
          obj.width = 150;
        } else {
          obj.title = item.fieldName;
        }
        return obj;
      });
      colums.push({
        title: "操作",
        dataIndex: "operate",
        align: "left",
        fixed: "right",
        width: 100,
        scopedSlots: { customRender: "operate" },
      });
      return colums;
    },
    async getTheadListApi() {
      const res = await getTheadList();
      if (res && res.code === 200) {
        let isShowList = res.result.filter((item) => item.isShow == 1);
        this.columns = this.disposeHeadData(isShowList);
      } else {
        let columns = [];
      }
    },
    async getTbodyDataApi() {
      const res = await getTbodyData({
        searchName: this.searchName,
        classLv1Code: this.classLv1Code,
        classLv2Code: this.classLv2Code,
        kpiCycle: this.kpiCycle,
        kpiTypeCodeList: this.kpiTypeCodeList,
        kpiCycleList: this.kpiCycleList,
        releaseTime: this.releaseTime,
        addDate: this.addDate || "",
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      });
      if (res && res.code === 200) {
        this.backKpiTypeCodeList = [...this.kpiTypeCodeList];
        this.backKpiCycleList = [...this.kpiCycleList];
        this.backAddDate = this.addDate;
        this.backEveryMonth = this.everyMonth;
        this.backEveryDay = this.everyDay;
        this.tableData = res?.result?.records || [];
        this.total = res?.result?.total || 0;
      } else {
        this.tableData = [];
        this.total = 0;
      }
    },
  },
});
